package com.tikou.ui.view.fallingview;

import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;

import java.util.BitSet;
import java.util.Random;

/**
 * Created by Tikou on 2017/12/25 14:41.
 * 作者：wangwei
 * 邮箱：ww@winning.com.cn
 * 说明：雪花属性的类
 * 参考：
 */

public class FallObject {
    private int initX;
    private int initY;
    private Random random;
    private int parentWidth;
    private int parentHeight;
    private float objectWidth;
    private float objectHeight;
    private int iniSpeed;
    public float presentX;
    public float presentY;
    public float presentSpeed;
    public int initSpeed;//初始下降速度
    private Bitmap bitmap;
    public ObjectBuilder builder;

    public FallObject(ObjectBuilder builder, int parentWidth, int parentHeight){
        random = new Random();
        this.parentWidth = parentWidth;
        this.parentHeight = parentHeight;
        initX = random.nextInt(parentWidth);//随机物体的X坐标
        initY = random.nextInt(parentHeight)- parentHeight;//随机物体的Y坐标，并让物体一开始从屏幕顶部下落
        presentX = initX;
        presentY = initY;

        initSpeed = builder.initSpeed;

        presentSpeed = initSpeed;
        bitmap = builder.bitmap;
        objectWidth = bitmap.getWidth();
        objectHeight = bitmap.getHeight();
    }

    public FallObject(ObjectBuilder builder){
        this.builder=builder;
        initSpeed=builder.initSpeed;
        bitmap=builder.bitmap;
    }

    public void drawObject(Canvas canvas, Paint paint){
        moveObject();
        canvas.drawBitmap(bitmap,presentX,presentY,paint);
    }
    /**
     * 移动物体对象
     */
    private void moveObject(){
        moveY();
        if(presentY>parentHeight){
            reset();
        }
    }

    /**
     * Y轴上的移动逻辑
     */
    private void moveY(){
        presentY += presentSpeed;
    }
    /**
     * 重置object位置
     */
    private void reset(){
        presentY = -objectHeight;
        presentSpeed = initSpeed;
    }

}
